package com.xiaoma.verbal.webservice;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import com.xiaoma.tpo.resource.TPOConstants;
import com.xiaoma.verbal.dao.PayAudioGrabDao;

@Controller
@RequestMapping("/service/xmpg")
public class XMPGWs {
    
    @Autowired
    private  RestTemplate restTemplate;
    
    @Autowired
    private PayAudioGrabDao payAudioGrabDao;

    @RequestMapping(value = "/getTeacherList", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getTeacherList(@RequestParam Integer audioId) {
        Map<String, Object> result = new HashMap<String, Object>();
        String teacherListUrl = TPOConstants.XMPG + "/xmpg/service/getTeacherList?moduleName=" + TPOConstants.MODULE_NAME + "&answerId=" + audioId;
        JSONObject json = restTemplate.getForObject(teacherListUrl, JSONObject.class);
        if (json.getInt("status") == 0) {//成功
            result.put("status", 1);
            result.put("teachers", json.getJSONArray("result"));
        } else {
            result.put("status", 0);
            result.put("message", json.getString("message"));
        }
        return result;
    }
    
    @RequestMapping(value = "/submitChecked", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> submitChecked(@RequestBody Map<String, Integer> params) {//提交选择的老师
        Map<String, Object> result = new HashMap<String, Object>();
        Integer audioId = params.get("audioId");
        Integer teacherId = params.get("teacherId");
        String submitTeacherUrl = TPOConstants.XMPG + "/xmpg/service/submitChecked";
        if (audioId != null && teacherId != null) {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<String> entity = new HttpEntity<String>(headers);
            ResponseEntity<JSONObject> responseEntity= restTemplate.exchange(submitTeacherUrl+"?moduleName=" + TPOConstants.MODULE_NAME + "&answerId=" + audioId + "&teacherId=" + teacherId, HttpMethod.POST, entity, JSONObject.class);
            JSONObject json = responseEntity.getBody();
            if (json.getInt("status") == 0) {//成功
                result.put("status", 1);
                payAudioGrabDao.comfirmGrab(audioId);
            } else {
                result.put("status", 0);
                result.put("message", json.getString("message"));
            }
        } else {
            result.put("status", 0); 
            result.put("message", "validate error"); 
        }
        
        return result;
    }
    
    
}
